using NetChinese.Interfaces;
using NetChinese.Models;

namespace NetChinese.Services
{
    /// <summary>
    /// 控制台用户界面管理器
    /// </summary>
    public class ConsoleUIManager : IUIManager
    {
        private static readonly string[] SUPPORTED_VERSIONS = { "net6.0", "net7.0", "net8.0", "net9.0" };
        private static readonly string[] COMPARE_MODES = { "OriginFirst", "LocaleFirst", "None" };
        private static readonly string[] SUPPORTED_LANGUAGES = { "zh-cn", "ja-jp", "ko-kr", "fr-fr", "de-de", "es-es", "ru-ru" };

        private static readonly Dictionary<string, string> LANGUAGE_DISPLAY_NAMES = new()
        {
            { "zh-cn", "简体中文" },
            { "ja-jp", "日语" },
            { "ko-kr", "韩语" },
            { "fr-fr", "法语" },
            { "de-de", "德语" },
            { "es-es", "西班牙语" },
            { "ru-ru", "俄语" }
        };

        private static readonly Dictionary<string, string> COMPARE_MODE_DISPLAY_NAMES = new()
        {
            { "OriginFirst", "原始内容在前" },
            { "LocaleFirst", "本地化内容在前" },
            { "None", "无对照" }
        };

        public async Task<string> SelectVersionAsync(string lastVersion)
        {
            return await SelectOptionAsync(
                SUPPORTED_VERSIONS,
                lastVersion,
                "请选择要安装的.NET版本:",
                version => version
            );
        }

        public async Task<string> SelectLanguageAsync(string lastLanguage)
        {
            return await SelectOptionAsync(
                SUPPORTED_LANGUAGES,
                lastLanguage,
                "请选择语言:",
                lang => LANGUAGE_DISPLAY_NAMES.GetValueOrDefault(lang, lang)
            );
        }

        public async Task<string> SelectCompareModeAsync(string lastCompareMode)
        {
            return await SelectOptionAsync(
                COMPARE_MODES,
                lastCompareMode,
                "请选择对照方式:",
                mode => COMPARE_MODE_DISPLAY_NAMES.GetValueOrDefault(mode, mode)
            );
        }

        private async Task<string> SelectOptionAsync(
            string[] options,
            string lastSelected,
            string prompt,
            Func<string, string> getDisplayName)
        {
            Console.WriteLine($"\n{prompt}");
            for (int i = 0; i < options.Length; i++)
            {
                var option = options[i];
                var isLastSelected = option == lastSelected;
                var displayName = getDisplayName(option);
                Console.WriteLine($"{i + 1}. {displayName} {(isLastSelected ? "[上次选择]" : "")}");
            }
            
            while (true)
            {
                Console.Write($"请输入选项(1-{options.Length}): ");
                if (int.TryParse(Console.ReadLine(), out int choice) && choice >= 1 && choice <= options.Length)
                {
                    return options[choice - 1];
                }
                Console.WriteLine("无效输入，请重新选择！");
            }
        }

        public async Task<bool> ShowSettingsAsync(Config config)
        {
            Console.WriteLine("\n设置：");
            Console.WriteLine($"1. 自动检查更新: {(config.AutoCheckUpdate ? "是" : "否")}");
            Console.WriteLine("2. 返回");

            Console.Write("\n请输入选项(1-2): ");
            if (!int.TryParse(Console.ReadLine(), out int choice))
            {
                Console.WriteLine("无效输入！");
                return false;
            }

            switch (choice)
            {
                case 1:
                    config.AutoCheckUpdate = !config.AutoCheckUpdate;
                    Console.WriteLine($"自动检查更新已{(config.AutoCheckUpdate ? "启用" : "禁用")}");
                    return true;
                case 2:
                    return false;
                default:
                    Console.WriteLine("无效输入！");
                    return false;
            }
        }

        public async Task<int> ShowMainMenuAsync()
        {
            Console.WriteLine("\n请选择操作：");
            Console.WriteLine("1. 安装/更新智能提示");
            Console.WriteLine("2. 管理缓存");
            Console.WriteLine("3. 自定义构建");
            Console.WriteLine("4. 设置");
            Console.WriteLine("0. 退出");

            Console.Write("\n请输入选项(0-4): ");
            if (!int.TryParse(Console.ReadLine(), out int choice))
            {
                Console.WriteLine("无效输入！");
                return -1;
            }

            return choice;
        }

        public async Task<bool> ShowCacheMenuAsync()
        {
            Console.WriteLine("\n缓存管理：");
            Console.WriteLine("1. 清理缓存");
            Console.WriteLine("2. 查看缓存状态");
            Console.WriteLine("3. 返回");
            return true;
        }
    }
} 